home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / Random.c < prev    next >
C/C++ Source or Header  |  1990-05-20  |  3KB  |  134 lines

  1. /* Random.c -- implementation of pseudo-random number generator
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     December, 1985
  21.  
  22. Function:
  23.     
  24. A psuedo-random number generator.  The function next() returns random
  25. numbers uniformly distributed over the interval (0.0,1.0).
  26.  
  27. Reference:
  28.  
  29. Pierre L'ecuyer, "Efficient and Portable Combined Random Number
  30. Generators", Commun. ACM 31, 6 (June 1988), 742-749.
  31.  
  32. $Log:    Random.c,v $
  33.  * Revision 3.0  90/05/20  17:40:49  kgorlen
  34.  * Release for 1st edition.
  35.  * 
  36. */
  37.  
  38. #include "Random.h"
  39. #include <time.h>
  40. #include "nihclIO.h"
  41.  
  42. #define    THIS    Random
  43. #define    BASE    Object
  44. #define BASE_CLASSES BASE::desc()
  45. #define MEMBER_CLASSES
  46. #define VIRTUAL_BASE_CLASSES Object::desc()
  47.  
  48. DEFINE_CLASS(Random,2,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Random.c,v 3.0 90/05/20 17:40:49 kgorlen Rel $",NULL,NULL);
  49.  
  50. const long s1max = 2147483562;
  51. const long s2max = 2147483398;
  52.  
  53. void Random::checkSeeds()
  54. {
  55.     if (sizeof(long) != 4) {
  56.         cerr << "\nRandom only works on 32-bit machines\n";
  57.         exit(1);
  58.     }
  59.     if (s1 < 1 || s1 > s1max) s1 = (ABS(s1) % s1max) + 1;
  60.     if (s2 < 1 || s2 > s2max) s2 = (ABS(s2) % s2max) + 1;
  61. }
  62.  
  63. Random::Random()
  64. {
  65.     time(&s1);
  66.     s2 = (long)this;
  67.     checkSeeds();
  68. }
  69.  
  70. Random::Random(long seed1, long seed2)
  71. {
  72.     s1 = seed1;  s2 = seed2;
  73.     checkSeeds();
  74. }
  75.  
  76. float Random::next()
  77. {
  78.     long Z,k;
  79.  
  80.     k = s1/53668;
  81.     s1 = 40014 * (s1 - k * 53668) - k * 12211;
  82.     if (s1 < 0) s1 += s1max + 1;
  83.  
  84.     k = s2/52774;
  85.     s2 = 40692 * (s2 - k * 52774) - k * 3791;
  86.     if (s2 < 0) s2 += s2max + 1;
  87.  
  88.     Z = s1 - s2;
  89.     if (Z < 1) Z += s1max;
  90.  
  91.     return Z * 4.656613E-10;
  92. }
  93.  
  94. void Random::deepenShallowCopy()    {}
  95.  
  96. unsigned Random::hash() const    { return (unsigned)this; }
  97.  
  98. bool Random::isEqual(const Object& ob) const   { return isSame(ob); }
  99.  
  100. void Random::printOn(ostream& strm) const
  101. {
  102.     strm << s1 << ' ' << s2;
  103. }
  104.  
  105. Random::Random(OIOin& strm)
  106.     : BASE(strm)
  107. {
  108.     strm >> s1 >> s2;
  109. }
  110.  
  111. void Random::storer(OIOout& strm) const
  112. {
  113.     BASE::storer(strm);
  114.     strm << s1 << s2;
  115. }
  116.  
  117. Random::Random(OIOifd& fd)
  118.     : BASE(fd)
  119. {
  120.     fd >> s1 >> s2;
  121. }
  122.  
  123. void Random::storer(OIOofd& fd) const
  124. {
  125.     BASE::storer(fd);
  126.     fd << s1 << s2;
  127. }
  128.  
  129. int Random::compare(const Object&) const
  130. {
  131.     shouldNotImplement("compare");
  132.     return 0;
  133. }
  134.